package com.banmao.algorithm.No1To100.No59;

/**
 *  labuladong 的题解可以轻易改成 n * m 的矩阵
 */
public class Solution1 {

    public int[][] generateMatrix(int n, int m) {

        int upper_border = 0;
        int left_border = 0;
        int right_border = m - 1;
        int bottom_border = n - 1;

        //int i = 0, j = 0;
        int value = 1;
        int[][] res = new int[n][m];

        while(value <= n * m) {
            //if(upper_border <= bottom_border) {
                for(int j = left_border; j <= right_border; j++) {
                    res[upper_border][j] = value++;
                }
                upper_border++;
            //}

            //if(left_border <= right_border) {
                for (int i = upper_border; i <= bottom_border; i++) {
                    res[i][right_border] = value++;
                }
                right_border--;
            //}

            //if(bottom_border >= upper_border) {
                for(int j = right_border; j >= left_border; j--) {
                    res[bottom_border][j] = value++;
                }
                bottom_border--;
            //}

            //if(right_border >= left_border) {
                for (int i = bottom_border; i >= upper_border; i--) {
                    res[i][left_border] = value++;
                }
                left_border++;
            //}

        }

        return res;

    }

}
